package com.casic.modules.sys.controller;


import com.casic.common.annotation.SysLog;
import com.casic.common.base.entity.PageEntity;
import com.casic.common.utils.*;
import com.casic.common.validator.group.UpdateGroup;
import com.casic.modules.sys.entity.SysUserEntity;
import com.casic.modules.sys.service.SysUserDeptService;
import com.casic.modules.sys.service.SysUserService;
import com.casic.modules.sys.shiro.ShiroUtils;
import com.casic.common.validator.Assert;
import com.casic.common.validator.ValidatorUtils;
import com.casic.common.validator.group.AddGroup;
import com.casic.modules.sys.service.SysUserRoleService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月31日 上午10:40:10
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysUserDeptService sysUserDeptService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        int currentPage = 1;
        int pageSize = 10;
        if (params.get("page") != null) {
            currentPage = Integer.parseInt((String) params.get("page"));
        }
        if (params.get("limit") != null) {
            pageSize = Integer.parseInt((String) params.get("limit"));
        }
        PageHelper.startPage(currentPage, pageSize);
        List<SysUserEntity> list = sysUserService.queryList(params);
        return R.ok().put("page", new PageEntity<SysUserEntity>(list));
    }

    /**
     * 企业选择民警
     *
     * @param params
     * @return
     */
    @RequestMapping("/getUserList")
    public R getUserList(@RequestParam Map<String, Object> params) {
        params.put("roleId", Constant.POLICE_ROLE_ID);
        PageEntity<SysUserEntity> data = sysUserService.findListParams(params);
        return R.ok().put("page", data);
    }

    /**
     * 获取登录的用户信息
     */
    @RequestMapping("/info")
    public R info() {
        SysUserEntity sysUserEntity = sysUserService.queryByUserName(getUser().getUsername());
        return R.ok().put("user", sysUserEntity);
    }

    /**
     * 修改登录用户密码
     */
    @SysLog("修改密码")
    @RequestMapping("/password")
    public R password(String password, String newPassword) {
        Assert.isBlank(newPassword, "新密码不为能空");

        //原密码
        password = ShiroUtils.sha256(password, getUser().getSalt());
        //新密码
        newPassword = ShiroUtils.sha256(newPassword, getUser().getSalt());

        //更新密码
        int count = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (count == 0) {
            return R.error("原密码不正确");
        }

        return R.ok();
    }

    /**
     * 用户信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") String userId) {
        SysUserEntity user = sysUserService.queryObject(userId);

        //获取用户所属的角色列表
        List<String> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        //获取用户所属的部门列表
        List<String> deptIdList = sysUserDeptService.queryDeptIdList(userId);
        user.setDeptIdList(deptIdList);

        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @RequestMapping("/save")
    public R save(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, AddGroup.class);
        sysUserService.save(user);
        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/update")
    public R update(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        sysUserService.update(user);

        return R.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] userIds) {
        if (ArrayUtils.contains(userIds, 1L)) {
            return R.error("系统管理员不能删除");
        }

        if (ArrayUtils.contains(userIds, getUserId())) {
            return R.error("当前用户不能删除");
        }

        sysUserService.deleteBatch(userIds);

        return R.ok();
    }

    @RequestMapping("/alllist")
    public R alllist(@RequestParam Map<String, Object> params) {
        List<SysUserEntity> list = sysUserService.queryList(params);
        return R.ok().put("page", list);
    }

    /**
     * 修改shiro中的用户部门
     *
     * @param
     * @return
     */
    @RequestMapping("/updateuser")
    public R updateuser(String deptId) {
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        sysUserEntity.setDeptId(deptId);
        ShiroUtils.setUser(sysUserEntity);
        return R.ok();
    }
}
